package com.gegong.vmware.workstation.service.impl;

import com.gegong.vmware.workstation.*;
import com.gegong.vmware.workstation.exceptions.VMwareWorkstationManagementException;
import com.gegong.vmware.workstation.service.VMPowerManagementService;
import com.google.common.base.Charsets;
import okhttp3.Request;
import okhttp3.RequestBody;


public class DefaultVMPowerManagementServiceImpl implements VMPowerManagementService {
    @Override
    public VMPowerState getPowerState(String id) throws VMwareWorkstationManagementException {

        String url = getUrl(VMwareWorkstationManagementActions.Power.GET_POWER_STATE, id);
        Request request = new Request.Builder()
                .url(url)
                .headers(defaultHeaders)
                .build();
        return getResponse(request, VMwareWorkstationManagementActions.Power.GET_POWER_STATE).getData(VMPowerState.class);
    }

    @Override
    public VMPowerState powerOperation(String id, VMPowerOperation operation) throws VMwareWorkstationManagementException {
        VMwareWorkstationManagementActions.BaseAction action = VMwareWorkstationManagementActions.Power.CHANGE_POWER_STATE;
        String url = getUrl(action, id);
        RequestBody requestBody = RequestBody.create(defaultPostMediaType, operation.toString().getBytes(Charsets.UTF_8));
        Request request = new Request.Builder()
                .url(url)
                .method(action.getMethod().toString(), requestBody)
                .headers(defaultPostHeaders)
                .build();
        return getResponse(request, action).getData(VMPowerState.class);
    }
}
